<?php
class HeadersController extends AppController{
	public $helpers = array('Html', 'Form');
	public $components = array('Session');

	function index(){
		$previous_data = $this->Header->find('all');
		$previous_data = $this->paginate('Header');
		// print_r($previous_data);
			// exit;
		if(count($previous_data)==0){
			$this->redirect(array('action' => 'add'));
		}else{
			$this->redirect(array('action' => 'edit',$previous_data[0]['Header']['id']));
		}
	}
	
	function edit($id=null){
		$this->loadModel('Upload');
		$base_url = 'http://'.$_SERVER['HTTP_HOST'].$this->base ;
		$this->set('base_url',$base_url);
		$dir = "upload/";
		$this->Header->id = $id;
		//echo "get".$this->request->is('get');

		if ($this->request->is('get')) {
			$this->request->data = $this->Header->read();
			$datas = $this->request->data;
			
			
			if(empty($datas['Header']['header_image'])){
				$datas['Header']['header_image']="blank_header.png";
			}
			
			if(empty($datas['Header']['logo'])){
				$datas['Header']['logo']="blank_logo.png";
			}
			
			$this->set('datas',$datas);
		} else{

			$datas = $this->request->data;
		
			$previous_data = $this->Header->read();
			
			$logo_image = $datas['Header']['logo'];
			$upload = $this->Upload->upload_image($logo_image,$dir);
			
			
			if($upload['status']){
				if($upload['name']==""){
					$datas['Header']['logo'] = $previous_data['Header']['logo'];
				}else{
					$datas['Header']['logo'] = $upload['name'];
				}
				
			}else{
				$this->Session->setFlash('Invalid Image.');
				$this->redirect(array('action' => 'index'));
			}
			
			//header image upload
			$header_image = $datas['Header']['header_image'];
			$upload = $this->Upload->upload_image($header_image,$dir);
			if($upload['status']){
				
				if($upload['name']==""){
					$datas['Header']['header_image'] = $previous_data['Header']['header_image'];
				}else{
					$datas['Header']['header_image'] = $upload['name'];
				}
				
			}else{
				$this->Session->setFlash('Invalid Image.');
				$this->redirect(array('action' => 'index'));
			}
			
			if ($this->Header->save($datas)) {
				$this->Session->setFlash('Your post has been updated.');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash('Unable to update your post.');
			}
		}
	}

	function add(){
		$this->loadModel('Upload');
		$base_url = 'http://'.$_SERVER['HTTP_HOST'].$this->base ;
		
		$dir = "upload/";
		
		if($this->request->is('post')){ 
		
			$datas = $this->request->data;
			
			print "<pre>";
			print_r($datas);
			print "</pre>";
			exit();
			
			//logo image upload
			$logo_image = $datas['Header']['logo'];
			$upload = $this->Upload->upload_image($logo_image,$dir);
			if($upload['status']){
				$datas['Header']['logo'] = $upload['name'];
			}else{
				$this->Session->setFlash('Invalid Image.');
				$this->redirect(array('action' => 'index'));
			}
			
			//header image upload
			$header_image = $datas['Header']['header_image'];
			$upload = $this->Upload->upload_image($header_image,$dir);
			if($upload['status']){
				$datas['Header']['header_image'] = $upload['name'];
			}else{
				$this->Session->setFlash('Invalid Image.');
				$this->redirect(array('action' => 'index'));
			}
		
			if( $this->Header->save($datas)){
				 $this->Session->setFlash('Your post has been saved.');
				 $this->redirect(array('action' => 'index'));
			}
			
		}
	}
	
	function image_delete(){
        $this->autoRender = false;
		$datas = $this->request->data;
		
		$update = array("Header"=>array(
					"id"=>$datas['id'],
					$datas['type']=>"",
				));
		$this->Header->save($update);
        //print_r($this->request->data);
        print_r($update);
        
	}
	
	
	
	
}
?>